<?php
class occam
{
// Private
var $socket;

// Public
var $hostaddr;
var $password1;
var $password2;
var $bladeid;

function occam($hostaddr,$password1,$password2,$bladeid)
{
$this->hostaddr = $hostaddr;
$this->password1 = $password1;
$this->password2 = $password2;
$this->bladeid = $bladeid;
}

function opentelnetsession()
{
// This is the difficult part, the Telnet header
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);

// connecting
$this->socket=fsockopen($this->hostaddr,23,&$errno, &$errstr,3);

// sending the Telnet header
fputs($this->socket,$header1);
usleep(125000);
fputs($this->socket,$header2);
usleep(125000);

// login
fputs($this->socket,"$this->password1\r");
usleep(125000);
fputs($this->socket,"en\r");
usleep(125000);
fputs($this->socket,"$this->password2\r");
usleep(125000);
fputs($this->socket,"terminal length 0\r");
usleep(125000);
fputs($this->socket,"terminal width 100\r");
usleep(125000);
}

function sendcommand($command,$sleep)
{
fputs($this->socket,"$command\r");
if ($sleep == "0")
{sleep(1); }
else
{sleep($sleep); }
}

function eqn_lookup($ig,$crv,$sleep)
{
fputs($this->socket,"show interface-group $ig $crv\r");
usleep(125000);
if ($sleep == "0")
{
	sleep(1);
} else {
	sleep($sleep);
}
}

function occam_add_customer($port,$type,$subName,$subPhone,$subCablePair,$dsl_profile,$access_profile,$video,$sleep)
{
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
if ($type == "ethernet" && $port < 7) {
	fputs($this->socket,"medium fiber\r");
	usleep(125000);
}
fputs($this->socket,"no info login\r");
usleep(125000);
fputs($this->socket,"info subscriber $subName\r");
usleep(125000);
fputs($this->socket,"info circuit-id $subPhone pair $subCablePair\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"dsl profile $dsl_profile\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"shutdown\r");
	usleep(125000);
	fputs($this->socket,"eps access subscriber\r");
	usleep(125000);
	fputs($this->socket,"no shutdown\r");
	usleep(125000);
}
fputs($this->socket,"service 1\r");
usleep(125000);
fputs($this->socket,"shutdown\r");
usleep(125000);
fputs($this->socket,"access profile $access_profile\r");
usleep(125000);
if ($type == "ethernet") {
	fputs($this->socket,"no match vlan 4\r");
	usleep(125000);
	fputs($this->socket,"match vlan 4\r");
	usleep(125000);
	fputs($this->socket,"override profile mac limit 2\r");
	usleep(125000);
}
fputs($this->socket,"no shutdown\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	if ($video == "True") {
		fputs($this->socket,"service 2\r");
		usleep(125000);
		fputs($this->socket,"no shutdown\r");
		usleep(125000);
		fputs($this->socket,"exit\r");
		usleep(125000);
	}
	else {
		fputs($this->socket,"service 2\r");
		usleep(125000);
		fputs($this->socket,"shutdown\r");
		usleep(125000);
		fputs($this->socket,"exit\r");
		usleep(125000);
	}
}
else if ($type == "ethernet") {
	if ($video == "True") {
		fputs($this->socket,"service 3\r");
		usleep(125000);
		fputs($this->socket,"shutdown\r");
		usleep(125000);
		fputs($this->socket,"access profile VIDEO\r");
		usleep(125000);
		fputs($this->socket,"no match vlan 3\r");
		usleep(125000);
		fputs($this->socket,"match vlan 3\r");
		usleep(125000);
		fputs($this->socket,"override profile mac limit 5\r");
		usleep(125000);
		fputs($this->socket,"no shutdown\r");
		usleep(125000);
		fputs($this->socket,"exit\r");
		usleep(125000);
	}
	else {
		fputs($this->socket,"service 3\r");
		usleep(125000);
		fputs($this->socket,"shutdown\r");
		usleep(125000);
		fputs($this->socket,"access profile VIDEO\r");
		usleep(125000);
		fputs($this->socket,"match vlan 3\r");
		usleep(125000);
		fputs($this->socket,"override profile mac limit 5\r");
		usleep(125000);
		fputs($this->socket,"exit\r");
		usleep(125000);
	}
	fputs($this->socket,"service 2\r");
	usleep(125000);
	fputs($this->socket,"shutdown\r");
	usleep(125000);
	fputs($this->socket,"access profile VOICE-ONT\r");
	usleep(125000);
  	fputs($this->socket,"no match vlan 2\r");
	usleep(125000);
  	fputs($this->socket,"match vlan 2\r");
	usleep(125000);
  	fputs($this->socket,"override profile mac limit 16\r");
	usleep(125000);
  	fputs($this->socket,"no shutdown\r");
	usleep(125000);
	fputs($this->socket,"exit\r");
	usleep(125000);
	
}
fputs($this->socket,"no shutdown\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
//Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0")
{
	sleep(1);
}
else
{
	sleep($sleep);
}

}

function suspend_internet($port,$type,$sleep)
{
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
fputs($this->socket,"service 1\r");
usleep(125000);
fputs($this->socket,"shutdown\r");
usleep(125000);
// Exit from service
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0")
{
	sleep(1);
}
else
{
	sleep($sleep);
}
}

function suspend_video($port,$type,$sleep)
{
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"service 2\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"service 3\r");
	usleep(125000);
}
fputs($this->socket,"shutdown\r");
usleep(125000);
// Exit from service
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0")
{
	sleep(1);
}
else
{
	sleep($sleep);
}
}

function restore_video($port,$type,$sleep)
{

//TODO:  If dsl profile is not TV, then leave service 2 shutdown
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"service 2\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"service 3\r");
	usleep(125000);
}
fputs($this->socket,"no shutdown\r");
usleep(125000);
// Exit from service
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0")
{
	sleep(1);
}
else
{
	sleep($sleep);
}
}

function restore_internet($port,$type,$sleep)
{

//TODO:  If dsl profile is not TV, then leave service 2 shutdown
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
fputs($this->socket,"service 1\r");
usleep(125000);
fputs($this->socket,"no shutdown\r");
usleep(125000);
// Exit from service
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function delete_dhcp_clients($port,$mac,$type,$sleep)
{

fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int dsl $port\r");
usleep(125000);
fputs($this->socket,"service 2\r");
usleep(125000);
fputs($this->socket,"delete dhcp client $mac\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
fputs($this->socket,"sh dhcp clients int dsl $port\r");
//  We don't need to save config for deletions
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function show_dsl_service($port,$sleep)
{
fputs($this->socket,"sh dsl service interface atm $port\r");
usleep(125000);
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function show_multicast_info($port,$type,$sleep)
{
fputs($this->socket,"sh multicast port $type $port\r");
usleep(125000);
if ($sleep == "0"){
	sleep(1);
} else {
	sleep($sleep);
}
}

function show_dsl_sum($port,$sleep)
{
fputs($this->socket,"sh dsl sum $port\r");
usleep(125000);
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function shut_no_shut($port,$sleep)
{
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int dsl $port\r");
usleep(125000);
fputs($this->socket,"shutdown\r");
//TODO:  THis may need to be increased
usleep(125000);
fputs($this->socket,"no shutdown\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
fputs($this->socket,"exit\r");
if ($sleep == "0") {
	sleep(1);
} else {
	sleep($sleep);
}
}

function show_dhcp_clients($port,$type,$sleep)
{

fputs($this->socket,"sh dhcp clients int $type $port\r");
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function port_performance($port,$type,$sleep)
{
if ($type == "dsl") {
	fputs($this->socket,"sh dsl performance $port\r");
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet errors $port\r");
}
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function get_customer_info($port,$type,$sleep)
{

fputs($this->socket,"sh run int $type $port\r");
if ($sleep == "0"){
	sleep(1);
}
else{
	sleep($sleep);
}
}

function speed_change($port,$type,$cmd,$sleep)
{
fputs($this->socket,"con t\r");
usleep(125000);
fputs($this->socket,"int $type $port\r");
usleep(125000);
fputs($this->socket,"service 1\r");
usleep(125000);
fputs($this->socket,"shutdown\r");
usleep(125000);
fputs($this->socket,"access profile $cmd\r");
usleep(125000);
fputs($this->socket,"no shut\r");
usleep(125000);
// Exit from service
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from port
fputs($this->socket,"exit\r");
usleep(125000);
// Exit from con t
fputs($this->socket,"exit\r");
usleep(125000);
if ($type == "dsl") {
	fputs($this->socket,"sh dsl service interface atm $port\r");
	usleep(125000);
}
else if ($type == "ethernet") {
	fputs($this->socket,"sh int ethernet service $port\r");
	usleep(125000);
}
fputs($this->socket,"copy run start\r");
usleep(125000);
if ($sleep == "0"){
	sleep(1);
}
else
{
	sleep($sleep);
}
}

function getoutput()
{
// Change the next line from 138 to 1 to keep all header info intact in output
fread($this->socket, 146);
$stat=socket_get_status($this->socket);

while($stat["unread_bytes"])
{
$output.=fread($this->socket, 1);
$stat=socket_get_status($this->socket);
}
return "<pre>" . $output . "</pre>";
}

function closetelnetsession()
{
fputs($this->socket,"exit\r");
usleep(125000);
fputs($this->socket,"exit\r");
usleep(125000);
fclose($this->socket);
}
}
?> 
